const path = require('path');
const webpack = require('webpack');

const Uglify = require('uglifyjs-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

const ROOT_PATH = path.resolve(__dirname);

function resolve(dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  /*entry: './src/index.js',*/
  devtool: '#source-map',
  entry: {
    'index': './src/js/index.js',
    'payIndex': './src/js/payIndex.js',
    'promotions':'./src/js/promotions.js',
    'search': './src/js/search.js'
  },
  output: {
    filename: 'static/js/[name][hash].js',
    chunkFilename: 'static/js/[id].chunk.js',
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: "babel-loader"
      },
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: "style-loader",
          use: [
            {
              loader: 'css-loader',
              options: {
                minimize: true //css压缩
              }
            }
          ]
        })
      },
      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader', 'sass-loader']
        })
      },
      {
        test: /\.html$/,
        loader: "html-loader?attrs=img:src img:data-src"
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: 'static/img/[name].[hash:7].[ext]'
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: 'static/fonts/[name].[ext]'
        }
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin({
      filename: 'static/css/[name].css',
      disable: false,
      allChunks: true
    }),
    new HtmlWebpackPlugin(
        {
          "filename": "index.html",
          "template": "./src/index.html",
          "chuckName": "index",
          "chunks" : ['index']
        }
    ),
    new HtmlWebpackPlugin(
        {
          "filename": "payIndex.html",
          "template": "./src/payIndex.html",
          "chuckName": "payIndex",
          "chunks" : ['payIndex']
        }
    ),
    new HtmlWebpackPlugin(
      {
        "filename": "views/promotions.html",
        "template": "./src/promotions.html",
        "chuckName": "promotions",
        "chunks" : ['promotions']
      }
    ),
    new HtmlWebpackPlugin(
      {
        "filename": "views/search.html",
        "template": "./src/search.html",
        "chuckName": "search",
        "chunks" : ['search']
      }
    )
  ],
  devServer: {
    contentBase: './',
    host: 'localhost',
    compress: true,
    port: 3000,
    inline: true,
    open: true
  }
};

